package com.bawei.module_main.ui

import androidx.viewpager2.widget.ViewPager2.OnPageChangeCallback
import com.alibaba.android.arouter.facade.annotation.Route
import com.bawei.lib_common.Router
import com.bawei.lib_common.base.BaseActivity
import com.bawei.lib_common.base.BaseViewModel
import com.bawei.lib_storage.AppMMKV
import com.bawei.lib_storage.AuthMMKV
import com.bawei.module_main.R
import com.bawei.module_main.adapter.ViewPageAdapter
import com.bawei.module_main.databinding.ActivityMainBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlin.properties.Delegates

/**
 *author：yjk
 *description:
 *date：2025/6/11
*/
@Route(path = Router.MAIN_PAGE)
@AndroidEntryPoint
class MainActivity : BaseActivity<BaseViewModel,ActivityMainBinding>() {
    override val mLayoutResId: Int
        get() = R.layout.activity_main

    override fun initView() {
        super.initView()
        setupViewPager()
        setupBottomNavigation()
    }
    private fun setupViewPager() {
        val adapter: ViewPageAdapter = ViewPageAdapter(this)
        mBinding.viewPager.setAdapter(adapter)
        mBinding.viewPager.setOffscreenPageLimit(4) // 缓存所有页面


        // 禁止滑动
        // viewPager.setUserInputEnabled(false);

        // ViewPager页面切换监听
        mBinding.viewPager.registerOnPageChangeCallback(object : OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                // 同步BottomNavigationView选中状态
                mBinding.bottomNavigation.getMenu().getItem(position).setChecked(true)
            }
        })
    }
    private fun setupBottomNavigation() {
        mBinding.bottomNavigation.setOnNavigationItemSelectedListener { item ->
            val itemId: Int = item.getItemId()
            if (itemId == R.id.nav_home) {
                mBinding.viewPager.setCurrentItem(0, false)
                return@setOnNavigationItemSelectedListener true
            } else if (itemId == R.id.nav_service) {
                mBinding.viewPager.setCurrentItem(1, false)
                return@setOnNavigationItemSelectedListener true
            } else if (itemId == R.id.nav_message) {
                mBinding.viewPager.setCurrentItem(2, false)
                return@setOnNavigationItemSelectedListener true
            } else if (itemId == R.id.nav_notice) {
                mBinding.viewPager.setCurrentItem(3, false)
                return@setOnNavigationItemSelectedListener true
            } else if (itemId == R.id.nav_mine) {
                mBinding.viewPager.setCurrentItem(4, false)
                return@setOnNavigationItemSelectedListener true
            }
            false
        }
    }

    override fun onResume() {
        super.onResume()
    }
}